/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmQuanLyHoaDon.java
 *
 * Created on Aug 22, 2012, 3:05:42 PM
 */
package gui;
import java.text.*;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.*;
import dao.*;
/**
 *
 * @author dtth
 */
public class frmQuanLyHoaDon extends javax.swing.JFrame {

    // Vùng khai báo biến
    private static Date dNgayLapHoaDon = new Date();
    private static Date dNgayDenNgay = new Date();
    private DefaultTableModel TableModel;
    private static Vector rowHeader = new Vector();
    private static int iSoLuongSP = 1;
    private static int iTrangThaiHoaDon = 0;
    // Kết thúc Vùng khai báo biến
    
    /// Vùng xây dựng các phương thức
    public void CreateHeaderTableSanPham() {
        try {
            TableModel = new DefaultTableModel(new Vector(),0);
            rowHeader.addElement("STT");    
            rowHeader.addElement("Mã HĐ");
            rowHeader.addElement("Tên HĐ");    
            rowHeader.addElement("Thành Tiền");
            rowHeader.addElement("Ngày Lập");    
            rowHeader.addElement("Trạng Thái");    
            rowHeader.addElement("Nhân Viên");
            
            TableModel = new DefaultTableModel(rowHeader, 0);
            jTableListHoaDon.setModel(TableModel);
        } catch(Exception ex) {
            
        }
    }
    
    public void CreateTableSanPham(Date prStartDate, Date prEndDate) {
        try {
            for (int i = TableModel.getRowCount() - 1; i >= 0; i-- ){
                TableModel.removeRow(i);
                iSoLuongSP = 1;
            }
            List<Hoadon> listHD = null;
            if(iTrangThaiHoaDon!=0) {
                if(iTrangThaiHoaDon==1)
                    listHD = HoaDonDAO.TimKiemHoaDon(prStartDate, prEndDate, true);
                else
                    listHD = HoaDonDAO.TimKiemHoaDon(prStartDate, prEndDate, false);
            } else {
                listHD = HoaDonDAO.TimKiemHoaDon(prStartDate, prEndDate);
            }
            for (int i = 0; i < listHD.size(); i++) {
                Hoadon hd = listHD.get(i);
                Vector row = new Vector();
                row.addElement(iSoLuongSP);
                row.addElement(hd.getMahoadon());
                row.addElement(hd.getTenhoadon());
                row.addElement(hd.getThanhtien());
                row.addElement(hd.ConvertNgayLapHD());
                row.addElement(hd.ConvertTrangThai());
                
                Nhanvien nv = hd.getNhanvien();
                row.addElement(nv.getTennhanvien());
                
                TableModel.addRow(row);
                iSoLuongSP++;
            }
            jTableListHoaDon.setModel(TableModel);
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, ex.toString(),"Loi",JOptionPane.INFORMATION_MESSAGE);
        }
    }
    /// Kết thúc vùng xây dựng các phương thức
    
    /** Creates new form frmQuanLyHoaDon */

    public frmQuanLyHoaDon() {
        initComponents();
        
        this.jdateNgayLapHD.setDate(new Date());
        this.jdateNgayDenNgay.setDate(new Date());
        dNgayLapHoaDon = jdateNgayLapHD.getDate();
        dNgayDenNgay = jdateNgayDenNgay.getDate();
        
        this.jdateNgayLapHD.getDateEditor().setEnabled(false);
        this.jdateNgayDenNgay.getDateEditor().setEnabled(false);
        
        this.jrbtnTatCa.setSelected(true);
        
        CreateHeaderTableSanPham();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableListHoaDon = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jdateNgayLapHD = new com.toedter.calendar.JDateChooser();
        jdateNgayDenNgay = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        jbtnXemHD = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jrbtnDaThanhToan = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jrbtnChuaThanhToan = new javax.swing.JRadioButton();
        jrbtnTatCa = new javax.swing.JRadioButton();
        jbtnDong = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("QUẢN LÝ HÓA ĐƠN");
        jLabel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jTableListHoaDon.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jTableListHoaDon.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableListHoaDon.setFillsViewportHeight(true);
        jScrollPane1.setViewportView(jTableListHoaDon);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Ngày lập HĐ:");

        jdateNgayLapHD.setDateFormatString("dd/MM/yyyy");
        jdateNgayLapHD.setFont(new java.awt.Font("Tahoma", 0, 12));
        jdateNgayLapHD.setPreferredSize(new java.awt.Dimension(100, 20));
        jdateNgayLapHD.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jdateNgayLapHDPropertyChange(evt);
            }
        });

        jdateNgayDenNgay.setDateFormatString("dd/MM/yyyy");
        jdateNgayDenNgay.setFont(new java.awt.Font("Tahoma", 0, 12));
        jdateNgayDenNgay.setPreferredSize(new java.awt.Dimension(100, 20));
        jdateNgayDenNgay.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jdateNgayDenNgayPropertyChange(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Đến ngày:");

        jbtnXemHD.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jbtnXemHD.setText("Xem");
        jbtnXemHD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnXemHDActionPerformed(evt);
            }
        });

        jRadioButton1.setText("jRadioButton1");

        buttonGroup1.add(jrbtnDaThanhToan);
        jrbtnDaThanhToan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbtnDaThanhToan.setText("Đã thanh toán");
        jrbtnDaThanhToan.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jrbtnDaThanhToanItemStateChanged(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Trạng thái HĐ");

        buttonGroup1.add(jrbtnChuaThanhToan);
        jrbtnChuaThanhToan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbtnChuaThanhToan.setText("Chưa thanh toán");
        jrbtnChuaThanhToan.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jrbtnChuaThanhToanItemStateChanged(evt);
            }
        });

        buttonGroup1.add(jrbtnTatCa);
        jrbtnTatCa.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbtnTatCa.setSelected(true);
        jrbtnTatCa.setText("Tìm tất cả");
        jrbtnTatCa.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jrbtnTatCaItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jrbtnTatCa)
                        .addGap(18, 18, 18)
                        .addComponent(jrbtnDaThanhToan)
                        .addGap(18, 18, 18)
                        .addComponent(jrbtnChuaThanhToan))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jdateNgayLapHD, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                        .addComponent(jdateNgayDenNgay, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)))
                .addGap(15, 15, 15)
                .addComponent(jbtnXemHD, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jdateNgayLapHD, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jdateNgayDenNgay, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jrbtnTatCa)
                    .addComponent(jrbtnDaThanhToan)
                    .addComponent(jrbtnChuaThanhToan)
                    .addComponent(jbtnXemHD, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jbtnDong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jbtnDong.setText("Đóng");
        jbtnDong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jbtnDong, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnDong, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12))
        );

        jLabel1.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jdateNgayDenNgayPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jdateNgayDenNgayPropertyChange
        // TODO add your handling code here:
        // Khi chọn ngày lập HD. 
        // Nếu trước ngày ngày đến thì ok
        // Ngược lại (sau ngày đến) thì giử nguyên giá trị và báo người dùng chọn lại
        
        dNgayDenNgay = jdateNgayDenNgay.getDate();
//        if(dNgayDenNgay!= null && jdateNgayDenNgay!=null){
//            if(dNgayLapHoaDon.after(dNgayDenNgay)){
//                return;
//                //JOptionPane.showMessageDialog(this, "OK", "Thông báo", JOptionPane.ERROR_MESSAGE);
//            }
//        }
    }//GEN-LAST:event_jdateNgayDenNgayPropertyChange

    private void jdateNgayLapHDPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jdateNgayLapHDPropertyChange
        // TODO add your handling code here:
        // Khi chọn ngày đến. 
        // Nếu sau ngày lập thì OK
        // Ngược lại (trước ngày lập) thì giử nguyên giá trị và báo người dùng chọn lại
        dNgayLapHoaDon = jdateNgayLapHD.getDate();
//        if(dNgayDenNgay!= null && dNgayLapHoaDon!=null){
//            if(dNgayLapHoaDon.after(dNgayDenNgay)){
//                return;
//                //JOptionPane.showMessageDialog(this, "OK", "Thông báo", JOptionPane.ERROR_MESSAGE);
//            }
//        }
    }//GEN-LAST:event_jdateNgayLapHDPropertyChange

    private void jrbtnDaThanhToanItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jrbtnDaThanhToanItemStateChanged
        // TODO add your handling code here:
        if(jrbtnDaThanhToan.isSelected())
            iTrangThaiHoaDon = 1;
            //JOptionPane.showMessageDialog(this, "Đã thanh toán", "Thông báo", JOptionPane.ERROR_MESSAGE);
    }//GEN-LAST:event_jrbtnDaThanhToanItemStateChanged

    private void jbtnDongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDongActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jbtnDongActionPerformed

    private void jrbtnChuaThanhToanItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jrbtnChuaThanhToanItemStateChanged
        // TODO add your handling code here:
        if(jrbtnChuaThanhToan.isSelected())
            iTrangThaiHoaDon = 2;
    }//GEN-LAST:event_jrbtnChuaThanhToanItemStateChanged

    private void jbtnXemHDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnXemHDActionPerformed
        // TODO add your handling code here:
        //JOptionPane.showMessageDialog(this, "Đã chọn", "Thông báo", JOptionPane.ERROR_MESSAGE);
        try {
            Date dStartDate = new Date();
            Date dEndDate = new Date();
            if(dNgayLapHoaDon.after(dNgayDenNgay)) {
                dStartDate = dNgayDenNgay;
                dEndDate = dNgayLapHoaDon;
            } else {
                dStartDate = dNgayLapHoaDon;
                dEndDate = dNgayDenNgay;
            }
            
            CreateTableSanPham(dStartDate, dEndDate);
        } catch (Exception ex) {
            
        }
    }//GEN-LAST:event_jbtnXemHDActionPerformed

    private void jrbtnTatCaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jrbtnTatCaItemStateChanged
        // TODO add your handling code here:
        iTrangThaiHoaDon = 0;
    }//GEN-LAST:event_jrbtnTatCaItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmQuanLyHoaDon.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmQuanLyHoaDon().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableListHoaDon;
    private javax.swing.JButton jbtnDong;
    private javax.swing.JButton jbtnXemHD;
    private com.toedter.calendar.JDateChooser jdateNgayDenNgay;
    private com.toedter.calendar.JDateChooser jdateNgayLapHD;
    private javax.swing.JRadioButton jrbtnChuaThanhToan;
    private javax.swing.JRadioButton jrbtnDaThanhToan;
    private javax.swing.JRadioButton jrbtnTatCa;
    // End of variables declaration//GEN-END:variables
}
